(function ($) {
	operModeSetup()
	function scoreTextTapped(){
		var textDom=this
		removeActive()
		var liDom = textDom.parentNode.parentNode
		currLiId = liDom.id
		liDom.classList.add('dhc-active')
		liDom.classList.add('text-active')
		opBoard.showDHCKeyboard();
		mui('#wrapper').scroll().reLayout();
		var index = parseInt(liDom.id)
		scrollToNewActiveScorLi(liDom);
	}
	
	function computeScore() {
		var items = paperData['SubItems']
		var realScore = 0;
		for(var i = 0; i < items.length; i++) {
			var itemDict = items[i];
			if(itemDict['HasChild'] == '1') {
				continue;
			}
			var itemScore = itemDict['ItemScore']
			if (itemScore == '') {
				continue
			}
			var num = parseFloat(itemScore);
			if (isNaN(num)) {
				continue
			}
			realScore += num
		}
		realScore = keepTwoDecimalPlaces(realScore); //--公共方法
		mui('#totalScore')[0].innerText = realScore;
		checkScore = realScore
	}
	function dealhlBoardScore(scoreStr,rowIndex){
		var valueStr = scoreStr +'';
		if (scoreStr == undefined) {
			valueStr = '';
		}
		var arr = valueStr.split('.')
		if (arr.length > 1) {
			if (arr[1] == '' || arr[1] == '0' || arr[1] == '00') {
				var index = valueStr.indexOf('.')
				valueStr = valueStr.substring(0,index);
			}
		}
		if (valueStr == '') {
			valueStr = '0'
		}
		paperData['SubItems'][rowIndex]['ItemScore'] = valueStr
		return valueStr
	}
	
	function dhcKeyboardBlock(type,textStr){
		var activeLi = mui('#scroll li.dhc-active')[0];
		if(!activeLi) {
			hideScoringPopover()
			return;
		}
		var rowIndex = parseInt(activeLi.id)
		var itemDict =paperData['SubItems'][rowIndex];
		var scoreText = activeLi.querySelector('.scoreText')
		var scoreTextStr = scoreText.innerText
		if (type == 'add') {
			var SubScore =  paperData['SubItems'][rowIndex]['SubScore']
			var score = parseFloat(SubScore)
			var oldStr = scoreTextStr;
			var oldVal = parseFloat(oldStr);
			var newStr = oldStr + textStr;
			var newVal = parseFloat(newStr);
			var dianIndex = oldStr.indexOf('.');
			if(oldStr == '0' || oldStr == '') {
				if(textStr == '.') {
					newStr = '0.';
				} else if(parseFloat(textStr) <= score) {
					newStr = textStr;
				} else {
					newStr = score;
				}
				scoreText.innerText = newStr;
			} else if(textStr == '.') {
				if(dianIndex == -1) {
					if (oldVal > score) {
						scoreText.innerText = '0.';
					}else{
						scoreText.innerText = newStr;
					}
					scoreText.innerText = newStr;
				}
				
			}else if(oldStr == SubScore+''){
				var temp = parseFloat(textStr);
				if (temp > score) {
					temp = score;
				}
				newStr = temp;
				scoreText.innerText = newStr;
			}else if(dianIndex != -1 && dianIndex == oldStr.length - 3) {
				//有小数点 并且是倒数第三位
				mui.toast('只能有两位小数哦~~')
				return;
			} else if(newVal > score) {
				newStr = score;
				scoreText.innerText = newStr;
			}else{
				scoreText.innerText = newStr;
			}
			dealhlBoardScore(newStr,rowIndex);
			computeScore();
		} else if(type == 'reduce') { //删除
			if(scoreTextStr != '') {
				var newScore = scoreTextStr.substring(0, scoreTextStr.length - 1);
				scoreText.innerText = newScore;
				dealhlBoardScore(newScore,rowIndex);
				computeScore()
			}
		}else if (type == 'mark') {
			scoreText.innerText = dealhlBoardScore(scoreTextStr,rowIndex);
			activeLi.classList.remove('dhc-active')
			mui.trigger(activeLi.querySelector('input'),'tap')
		}else if (type == 'last') {//上一个
			scoreText.innerText = dealhlBoardScore(scoreTextStr,rowIndex);
			choseLastScoreText()
		}else if (type == 'next') {//下一个
			scoreText.innerText = dealhlBoardScore(scoreTextStr,rowIndex);
			choseNextScoreText()
		}else if (type == 'close') {//关闭
			scoreText.innerText = dealhlBoardScore(scoreTextStr,rowIndex);
			hideScoringPopover();
			currLiId = ''
		}
	}
	function operModeSetup(){
		mui('#scroll .scoreText').each(function (index,element) {
			element.addEventListener('tap',scoreTextTapped)
		})
		if (!opBoard ) {
			opBoard = new mui.DHCKeyboard('scroll','',function(type, text) {
				dhcKeyboardBlock(type, text);
			});
			//opBoard.openDHCKeyboardDrag();
		}
	}
})(mui)